package aima.search;

import java.lang.*;
import java.util.*;
import aima.cs.util.*;

/** 
   This class implements depth-bounded search.
*/

public class DepthBoundedSearch implements SearchMethod {

/**
   Stack for depth-first search.
*/
   protected LIFOQueue Q; 

/**
   Bound on depth.
*/
   int maxDepth;

/**
   Constructor takes an initial problem state and depth bound.
*/

   public DepthBoundedSearch(State startState, int maxDepth) {
         Q = new LIFOQueue(); 
         Q.add( new SearchNode(startState) );

         this.maxDepth = maxDepth;
    }

/**
   Performs depth-bounded search from initial state.
*/

   public SearchNode search() {

      while (!Q.isEmpty()) {

         SearchNode expandNode = (SearchNode) Q.removeFront();
         if (expandNode.getState().isGoal()) {
             return expandNode;
         } else if (expandNode.getDepth() < maxDepth) {
             expandNode.expand(Q);
         }

      }

      return null;

   } 

}

